#include <stdio.h>
#include <conio.h>
#define MaxLength 30
//khai bao danh sach dac
typedef int ElementType;
typedef int Position;
typedef struct
{
        ElementType Elements[MaxLength];
        Position Last;
}List;
//tao danh sach rong
void MakeNull_List(List *L)
{
     L->Last = 0;
}
//ham kiem tra danh sach rong
int Empty_List(List L)
{
    return (L.Last == 0);
}
int Full_List(List L)
{
    return (L.Last == MaxLength);
}
//ham tra ve vi tri phan tu dau tien
Position FirstList(List L)
{
         return 1;
}
//ham tra ve vi tri sau phan tu cuoi trong danh sach
Position EndList(List L)
{
         return L.Last + 1;
}
//ham tra ve vi tri phan tu ke tiep phan tu P trong danh sach
Position Next(Position P, List L)
{
         return P +1;
}
//ham tra ve vi tri phan tu truoc
Position Previous(Position P, List L)
{
         return P-1;
}
//ham tra ve noi dung phan tu
ElementType Retrieve(Position P, List L)
{
            return L.Elements[P-1];
}
//them phan tu co noi dung X vao danh sach
void Insert_List(ElementType X, Position P, List *L)
{
     int i = 0;
     if(L->Last == MaxLength)   printf("\nDanh sach day!");
     else if((P<1) || (P>L->Last+1))           printf("\nVi tri khong hop le!");
     else
     {
         for(i=L->Last; i >= P; i--)
                        L->Elements[i] = L->Elements[i-1];
         L->Last++;
         L->Elements[P-1] = X;
     }
}
//xoa phan tu khoi danh sach
void Delete_List(Position P, List *L)
{
     if((P>L->Last) || (P<1))
                    printf("\nVi tri khong hop le");
     else
     {
         if(Empty_List(*L))
                           printf("\nDanh sach rong!");
         else
         {
             Position i;
             for(i=P;i<L->Last;i++)
             {
                                   L->Elements[i-1] = L->Elements[i];
             }
             L->Last--;
         }
     }
}
//in danh sach ra man hinh
void Print_List(List L)
{
     Position P;
     P = FirstList(L);
     printf("\nBat dau in danh sach: ");
     while(P != EndList(L))
     {
             printf("\n%d",Retrieve(P,L));
             P  = Next(P,L);
     }
     printf("\nKet thuc in danh sach!");
}
//nhap danh sach tu ban phim
void Read_List(List *L)
{
     int i, N;
     ElementType X;
     MakeNull_List(L);
     printf("\nNhap vao so phan tu trong danh sach: ");
     scanf("%d",&N);
     fflush(stdin);
     for(i=1;i<=N;i++)
     {
                      printf("\nPhan tu thu %d la: ",i);
                      scanf("%d",&X);fflush(stdin);
                      Insert_List(X,EndList(*L),L);
     }
}
